﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using BLL;
using DevExpress.Web.ASPxEditors;
using Entities;
namespace Stationery.UC
{
    public partial class UC_ProductList : System.Web.UI.UserControl
    {
        BLL_Product bll_Product = new BLL_Product();
        protected void Page_Load(object sender, EventArgs e)
        {
            //string strID = HttpUtility.ParseQueryString(uri.Query).Get("id");
            //if (!IsPostBack)
            //{
                if (Request.Params["id"] != null)
                {
                    string strID = Request.Params["id"].ToString();

                    ASPxDataViewProduct.DataSource = bll_Product.Get_Product_ID_MenuURL(Convert.ToInt32(strID));
                    ASPxDataViewProduct.DataBind();
                }
                else
                {
                    if (Request.RawUrl.ToUpper().Contains(Utils.web_home))//trang chu thi hien san pham nhap gan nhat
                    {
                        ASPxDataViewProduct.DataSource = bll_Product.Get_Product_ID_MenuURL(Convert.ToInt32(49));//load rau ăn lá//bll_Product.Get_Product_TopNew();
                        ASPxDataViewProduct.DataBind();
                    }
                    else
                    {
                        if (Request.Params["name"] != null)
                        {
                            string strName = Request.Params["name"].ToString();
                            string strCondition = String.Format("CODE LIKE '%{0}%' OR NAME LIKE '%{0}%' OR DESCRIPTION LIKE '%{0}%' OR PRICE LIKE '%{0}%'", strName);
                            ASPxDataViewProduct.DataSource = bll_Product.Get_Product_Dynamic(strCondition);
                            ASPxDataViewProduct.DataBind();
                        }
                        else
                        {
                            ASPxDataViewProduct.DataSource = bll_Product.Get_Product_All_IsDisplay();
                            ASPxDataViewProduct.DataBind();
                        }
                    }
                }
            //}
        }

        protected string GetLinkDetail(object pID)
        {
            string strID_PARENT = "";
            int intID ;
            string strURL = "";
            intID =Convert.ToInt32(pID);
            Product product;
            try
            {
                product = bll_Product.Lst_Product(bll_Product.Get_Product(intID))[0];
                strID_PARENT = product.ID_MENUURL.ToString();
            }
            catch
            {
                return "";
            }
            strURL = String.Format("WebProduct.aspx?id={0}&id_child={1}", strID_PARENT, intID);
            return strURL;
        }
        protected string GetStatusStore(object pStatus)
        {
            try
            {
                if (pStatus.ToString() == "O")
                {
                    return "(hết hàng)";
                }
                else if (pStatus.ToString() == "C")
                {
                    return "(sắp có)";
                }
                else
                {
                    return "(còn hàng)";
                }
            }
            catch
            {
                return "(hết hàng)";
            }
        }
        protected string GetColorStatus(object pStatus)
        {
            try
            {
                if (pStatus.ToString() == "O")
                {
                    return "Red";
                }
                else
                {
                    return "Green";
                }
            }
            catch
            {
                return "Red";
            }
        }
        protected bool GetVisibleStatus(object pStatus)
        {
            try
            {
                if (pStatus.ToString() == "O" || pStatus.ToString() == "C")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        
        protected void ASPxButtonFastCart_Click(object sender, EventArgs e)
        {
            string strID = "";
            string strURL = "";
            strID = ((ASPxButton)sender).CommandArgument.ToString();
            Product product;
            try
            {
                product = bll_Product.Lst_Product(bll_Product.Get_Product(Convert.ToInt64(strID)))[0];
            }
            catch
            {
                return;
            }
            //
            Order order = new Order();
            order.ID = 0;
            order.ID_PRODUCT = product.ID;
            order.PRICE = product.PRICE;
            order.QUANTITY = 1;
            order.TAX = 0;
            UpdatePriceValue(order);
            order.TYPE_PAY = "0";
            order.PRODUCT_DETAIL = product;
            //
            List<Order> lstOrder = new List<Order>();
            if (Session[Utils.SESSION_CART] != null)
            {
                lstOrder = (List<Order>)Session[Utils.SESSION_CART];
                for (int i = 0; i < lstOrder.Count; i++)
                {
                    if (lstOrder[i].ID_PRODUCT == order.ID_PRODUCT)//da ton tai san pham trong gio hang roi thi cong don
                    {
                        lstOrder[i].QUANTITY += 1;
                        UpdatePriceValue(lstOrder[i]);
                        break;
                    }
                    if (i == lstOrder.Count - 1)//phan tu cuoi cung ma chua ton tai thi them moi
                    {
                        order.ID = i + 1;
                        lstOrder.Add(order);
                        break;
                    }
                }
            }
            else
            {
                lstOrder.Add(order);
            }
            Session[Utils.SESSION_CART] = lstOrder;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Đã thêm vào giỏ hàng');location.href = location.href;", true);
            //MessageBox.Show("Đã thêm vào giỏ hàng");
            //Response.Redirect(Request.RawUrl);
        }
        protected void ASPxButtonCart_Click(object sender, EventArgs e)
        {
            string strID = "";
            string strURL = "";
            strID = ((ASPxButton)sender).CommandArgument.ToString();
            Product product;
            try
            {
                product = bll_Product.Lst_Product(bll_Product.Get_Product(Convert.ToInt64(strID)))[0];
            }
            catch
            {
                return;
            }
            //
            Order order = new Order();
            order.ID = 0;
            order.ID_PRODUCT = product.ID;
            order.PRICE = product.PRICE;
            order.QUANTITY = 1;
            order.TAX = 0;
            UpdatePriceValue(order);
            order.TYPE_PAY = "0";
            order.PRODUCT_DETAIL = product;
            //
            List<Order> lstOrder = new List<Order>();
            if (Session[Utils.SESSION_CART] != null)
            {
                lstOrder = (List<Order>)Session[Utils.SESSION_CART];
                for (int i = 0; i < lstOrder.Count; i++)
                {
                    if (lstOrder[i].ID_PRODUCT == order.ID_PRODUCT)//da ton tai san pham trong gio hang roi thi cong don
                    {
                        lstOrder[i].QUANTITY += 1;
                        UpdatePriceValue(lstOrder[i]);
                        break;
                    }
                    if (i == lstOrder.Count - 1)//phan tu cuoi cung ma chua ton tai thi them moi
                    {
                        order.ID = i + 1;
                        lstOrder.Add(order);
                        break;
                    }
                }
            }
            else
            {
                lstOrder.Add(order);
            }
            Session[Utils.SESSION_CART] = lstOrder;
            Response.Redirect(UtilsURL.order);            
            //MessageBox.Show("Đã thêm vào giỏ hàng");
            //Response.Redirect(Request.RawUrl);
        }
        private void UpdatePriceValue(Order pOrder)
        {
            pOrder.VALUE = pOrder.PRICE * pOrder.QUANTITY;
            pOrder.TOTAL = pOrder.VALUE + pOrder.VALUE * pOrder.TAX;
        }

        protected void ASPxButtonSearch_Click(object sender, EventArgs e)
        {
            string strCondition = String.Format("CODE LIKE '%{0}%' OR NAME LIKE '%{0}%' OR DESCRIPTION LIKE '%{0}%' OR PRICE LIKE '%{0}%'", ASPxTextBox1.Text);
            ASPxDataViewProduct.DataSource = bll_Product.Get_Product_Dynamic(strCondition);
            ASPxDataViewProduct.DataBind();
        }

        //public string AvailableImage(string pURL)
        //{
        //    string strReturn = "";
        //    if (!String.IsNullOrEmpty(pURL))
        //    {
        //        if(pURL.ToUpper().Contains("HTTP:"))
        //        {
        //            ////link tu tren mạng
        //            //HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(pURL);//lam cham di rat nhieu, tham chi callback ko hoat dong
        //            //request.Method = "HEAD";

        //            //try
        //            //{
        //            //    request.GetResponse();
        //                strReturn = pURL;
        //            //}
        //            //catch
        //            //{
        //            //    strReturn = "~/Image/Common/no_image.jpg";
        //            //}
        //        }
        //        else
        //        {
        //            //link trên máy
        //            if (System.IO.File.Exists(Server.MapPath(pURL)))
        //            {
        //                strReturn = pURL;
        //            }
        //            else
        //            {
        //                strReturn = "~/Image/Common/no_image.jpg";
        //            }
        //        }
        //    }
        //    else
        //    {
        //        strReturn = "~/Image/Common/no_image.jpg";
        //    }
        //    return strReturn;
        //}
    }
}